/* ==================================================================   
 * Created [2009-4-27 下午11:32:55] by Jon.King 
 * ==================================================================  
 * TSS 
 * ================================================================== 
 * mailTo:jinpujun@hotmail.com
 * Copyright (c) Jon.King, 2009-2012 
 * ================================================================== 
*/

package com.jinhe.tss.component.recycle;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;

import com.jinhe.tss.core.util.XMLDocUtil;

/**
 * <p> RecycleConfig.java </p>
 * <p>
 * recycle配置信息管理类
 * </p>
 */
public class RecycleConfig {
    
    public static final String RECYCLE_CONFIG_FILE = "META-INF/recycleConfig.xml";

	/** key:id ---  value:RecycleResource 以ID为key值将resources存在Map中 */
	Map<Object, RecycleResource> recycleResourcesMap = new LinkedHashMap<Object, RecycleResource>();
    
    private RecycleConfig() { 
    }
    
    private static RecycleConfig config;
    
    /**
     * 获取一个缓存池管理类实例。singleTon
     * @return
     */
    public static RecycleConfig getInstance(){
        if(config == null){
            config = new RecycleConfig();
            config.init();
        }
        return config;
    }
    
    private void init() {
        Document configDoc = XMLDocUtil.createDoc(RECYCLE_CONFIG_FILE);
        List<Element> nodes = XMLDocUtil.selectNodes(configDoc, "recycle/resource");
        
        for( Element resourceNode : nodes ){
            RecycleResource recycleResource = new RecycleResource(resourceNode);
            
            recycleResourcesMap.put(recycleResource.getId(), recycleResource);
            recycleResourcesMap.put(recycleResource.getEntity(), recycleResource);
        }
    }
	
	public Collection<RecycleResource> getRecycleResources() {
		return new LinkedHashSet<RecycleResource>(recycleResourcesMap.values());
	}

	public static RecycleResource getRecycleSource(Object recycleResourceKey){
		return RecycleConfig.getInstance().recycleResourcesMap.get(recycleResourceKey);
	}
}
